package com.shire.shire_interface.common.utils;

import com.shire.shire_interface.model.WxReqInfo;
import com.shire.shire_interface.model.account.DeviceInfo;
import com.shire.shire_interface.model.account.WxStation;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WxReqUtils {
    private static ThreadLocal<WxReqInfo> contain = new ThreadLocal<WxReqInfo>();

    public static WxReqInfo getContain(){
        return contain.get();
    }

    public static void setContain(WxReqInfo WxInfo){
        contain.set(WxInfo);
    }

    public static HttpServletRequest getRequest(){
        return contain.get().getRequest();
    }

    public static HttpServletResponse getResponse(){
        return contain.get().getResponse();
    }

    public static DeviceInfo getDeviceInfo(){
        return contain.get().getDeviceInfo();
    }

    public static WxStation getWxStation(){
        return contain.get().getWxStation();
    }

    public static String getWxAccount(){
        return getWxStation()!=null ? getWxStation().getWxaccount():null;
    }

    public static String getIp(){

        String ip = getRequest().getHeader("X-Real-IP");
        return ip==null ? getRequest().getRemoteAddr():ip;
    }

    public static String getUa(){
        return getRequest().getHeader("User-Agent");
    }

    public static String getCookie(String key)
    {
        Cookie[] cks = getRequest().getCookies();
        if(null == cks)
            return "";
        for (Cookie cookie : cks) {
            if(cookie.getName().equals(key))
                return cookie.getValue();
        }
        return "";
    }
    public static void setCookie(String key,String value)
    {
        getResponse().addCookie(new Cookie(key, value));
    }
}
